import {baseURL} from '@/config';
import axios from 'axios';
import {Message} from "element-ui";

const request = axios.create({
    baseURL,
    timeout: 1000 * 60,
});

//请求拦截器
request.interceptors.request.use(
    config => {
        /* if (config.url !== '/login') {
            config.headers['Authorization'] = `Bearer ${localStorage.getItem('token')}`
            // config.data.append('token', localStorage.getItem('token'))
            // console.log('data :>> ', config.data);
        } */
        // let sign = localStorage.getItem('token')
        // let time = localStorage.getItem('time')
        // let key = CryptoJS.MD5(sign + time).toString()
        // console.log("sign",sign)
        // console.log('time',time)
        // console.log('key',key)
        // config.headers['code'] = sign
        // config.headers['sign'] = encodeURIComponent(CryptoJS.AES.encrypt(sign, key).toString())
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

// 设置请求接受拦截器
request.interceptors.response.use(response => {
        // // 设置接受数据之后，做什么处理
        // if (response.data.code === 50000) {
        //   Message.error(response.data.data)
        // }
        // return res
        switch (response.data.code) {
            case 200:
                return {...response.data, data: response.data.data}
            default:
                Message({
                    title: '错误',
                    type: 'error',
                    message: response.data.msg.toString(),
                    duration: 5 * 1000,
                })
                return new Object(null)
        }
    },

    err => {
        console.log('err', err)
        Message({
            title: '错误',
            type: 'error',
            message: err.response.data.msg.toString(),
            duration: 5 * 1000,
        })

        // 判断请求异常信息中是否含有超时timeout字符串
        if (err.message.includes('timeout')) {
            console.log('错误回调', err)
            // Message.error('网络超时')
        }
        if (err.message.includes('Network Error')) {
            console.log('错误回调', err)
            // Message.error('服务端未启动，或网络连接错误')
        }
        // if (err.response.status === 441) {
        // 	serves.request({
        // 		method: 'post',
        // 		url: '/panCode/resetSecret'
        // 	})
        // }
        return Promise.reject(err)
    })
// request.get = (options) => request({
//     method: 'get',
//     ...options
// })
// request.post = (options) => request({
//     method: 'post',
//     ...options,
// })
// request.del = (options) => request({
//     method: 'del',
//     ...options
// })
// request.put = (options) => request({
//     method: 'put',
//     ...options
// })


export default request
